<?php

function mytheme_comment($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment;

	global $commentcount, $page;
	if ((int)get_option('page_comments') === 1 && (int)get_option('thread_comments') === 1) {//开启嵌套评论和分页才启用
		if (!$commentcount) {//初始化楼层计数器
			$page = (!empty($in_comment_loop)) ? get_query_var('cpage') : get_page_of_comment($comment -> comment_ID, $args);
			//获取当前评论列表页码
			$cpp = get_option('comments_per_page');
			//获取每页评论显示数量
			if (!$post_id)
				$post_id = get_the_ID();
			if (get_option('comment_order') === 'desc') {//倒序
				$cnt = get_comments(array('status' => 'approve', 'parent' => '0', 'post_id' => $post_id, 'count' => true));
				if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp)))
					$commentcount = $cnt + 1;
				else
					$commentcount = $cpp * $page + 1;
			} else {
				$commentcount = $cpp * ($page - 1);
			}
		}
		if (!$parent_id = $comment -> comment_parent) {
			$commentcountText = '<span class="floor">';
			if (get_option('comment_order') === 'desc') {//倒序
				$commentcountText .= --$commentcount . '楼';
			} else {
				$commentcountText .= ++$commentcount . '楼';
			}
			$commentcountText .= '</span">';
		}
	}

	extract($args, EXTR_SKIP);

	if ('div' == $args['style']) {
		$tag = 'div';
		$add_below = 'li-comment';
	} else {
		$tag = 'li';
		$add_below = 'div-comment';
	}

	if (!$parent_id = $comment -> comment_parent) {
		$class = 'comment_main';
	} else {
		$class = 'comment_para';
	}
	echo '<' . $tag . ' class="' . $class . '" id="li-comment-' . get_comment_ID() . '">';
	if ('div' != $args['style']) {
		echo '<div id="div-comment-' . get_comment_ID() . '" class="comment_body">';
	}

	echo '<div class="comment_author">';

	if ($args['avatar_size'] != 0)
		echo get_avatar($comment, $args['avatar_size']);
	echo '<span class="author">' . $comment -> comment_author . '</span>';
	echo '<span class="time">' . timecomm($comment -> comment_date_gmt) . '</span>';
	edit_comment_link(__('编辑'), '', '');
	echo $commentcountText;
	//主评论楼层号
	echo '</div>';
	if ($comment -> comment_approved == '0') {
		echo '<p>您的评论正在等待审核！</p>';
	}
	comment_text();
	echo '<div class="comment_reply">';
	if ( get_option('comment_registration') && ! is_user_logged_in() ) {
	echo '<a rel="nofollow" class="comment-reply-login" href="'.login_url('qq').'">登录以回复</a>';
	}else{
	comment_reply_link(array_merge($args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] + 10000)));
	}
	echo '</div>';

	if ('div' != $args['style']) {
		echo '</div>';
	}
}


add_filter('comment_reply_link', 'add_nofollow', 420, 4);

function add_nofollow($link, $args, $comment, $post){
	return preg_replace( '/href=\'(.*(\?|&)replytocom=(\d+)#respond)/', 'href='.login_url('qq'), $link );
}

// 评论添加@，by Ludou
function comment_add_at($comment_text, $comment = '') {
	if ($comment -> comment_parent > 0) {
		$comment_text = '@<span class="comment"><a href="#li-comment-' . $comment -> comment_parent . '">' . get_comment_author($comment -> comment_parent) . '</a></span> ' . $comment_text;
	}

	return $comment_text;
}

add_filter('comment_text', 'comment_add_at', 20, 2);

//~ 保存网络图片到本地
function httpcopy($url, $file = '', $timeout = 60) {
	$file = empty($file) ? pathinfo($url, PATHINFO_BASENAME) : $file;
	$dir = pathinfo($file, PATHINFO_DIRNAME);
	!is_dir($dir) && @mkdir($dir, 0755, true);
	$url = str_replace('', '', $url);
	if (function_exists('curl_init')) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		$temp = curl_exec($ch);
		if (@file_put_contents($file, $temp) && !curl_error($ch)) {
			return $file;
		} else {
			return false;
		}
	} else {
		$opts = array('http' => array('method' => 'GET', 'header' => '', 'timeout' => $timeout));
		$context = stream_context_create($opts);
		if (@copy($url, $file, $context)) {
			return $file;
		} else {
			return false;
		}
	}
}
?>
